package cn.com.dev;

import cn.com.elite.Util;
import cn.com.kh.KHUtil;
import cn.com.kh.TxConfig;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: 获取Maven依赖组件坐标
 */
public class Dev_FixMavenData {
    private static final Log log = LogFactory.getLog(Dev_FixMavenData.class);
    private static JdbcTemplate jt = new JdbcTemplate(TxConfig.getDruidDataSource());

    public static void main(String[] args) throws Exception {

        // batchFix();
        MavenFixData();

    }

    // Fix-获取Maven依赖组件坐标
    public static void MavenFixData() throws Exception {
        String dealSQL = "select 序号, 开源组件包坐标 from ms_it_comment where 开源组件包坐标 = 'qrcode_swetake-1.0.jar'";
        // dealSQL = "select distinct 开源组件包坐标 from ms_it_comment where groupId is null";
        List<Map<String, Object>> selectCus = jt.queryForList(dealSQL);
        log.debug("Fix-获取Maven依赖组件坐标 :" + selectCus.size());

        // 获取Maven依赖组件坐标
        for (Map<String, Object> CusMap : selectCus) {
            String rec_no = KHUtil.GetMapV(CusMap, "序号");
            String jarFile = KHUtil.GetMapV(CusMap, "开源组件包坐标");

            // 去掉版本号,获取不必定准确的artifactId,用于关键字搜索
            String artifactIdSimple = jarFile.replaceAll("[-|_]\\d.*", "");
            // 获取不必定准确的version,用于搜索结果筛选
            String versionSimple = jarFile.substring(artifactIdSimple.length()).replaceFirst("[-|_]", "").replaceAll(".jar", "");
            log.debug("获取Maven依赖组件坐标 Jar文件：" + jarFile + " artifactIdSimple:" + artifactIdSimple + " versionSimple:" + versionSimple);

            String call_Body = "{\"action\":\"coreui_Search\",\"method\":\"read\",\"data\":[{\"page\":1,\"start\":0,\"limit\":300,\"filter\":[{\"property\":\"keyword\",\"value\":\"" + artifactIdSimple + "\"}]}],\"type\":\"rpc\",\"tid\":9}";

            HashMap<String, Object> headMap = new HashMap<>();
            headMap.put("Content-Type", "application/json");

            String get_Json = Util.doHttpPostUseHead("http://nexus.kuangheng.com/service/extdirect", call_Body, "UTF-8", 120000, 0, headMap);
            log.debug("获取Maven依赖组件坐标 结果：" + get_Json);

            JSONObject res_Json = JSONObject.fromObject(get_Json);

            JSONArray res_ObjectArr = res_Json.getJSONObject("result").getJSONArray("data");
            for (int i = 0; i < res_ObjectArr.size(); i++) {
                String groupId = KHUtil.GetSFJV(res_ObjectArr.getJSONObject(i), "group");
                String artifactId = KHUtil.GetSFJV(res_ObjectArr.getJSONObject(i), "name");
                String version = KHUtil.GetSFJV(res_ObjectArr.getJSONObject(i), "version");
                // log.debug("获取Maven依赖组件坐标 Loop:" + res_ObjectArr.getJSONObject(i).toString());

                if (artifactIdSimple.equals(artifactId) && versionSimple.equals(version)) {
                    String Maven_Info = "<dependency>\n<groupId>" + groupId + "</groupId>\n<artifactId>" + artifactId + "</artifactId>\n<version>" + version + "</version>\n</dependency>";
                    log.debug("获取Maven依赖组件坐标 找到目标:" + Maven_Info);

                    jt.update("update ms_it_comment set groupId= ?,artifactId= ?,version= ?,Maven_Info= ? where 开源组件包坐标=?", groupId, artifactId, version, res_ObjectArr.getJSONObject(i).toString(), jarFile);
                    break;
                }

            }


            for (int i = 0; i < res_ObjectArr.size(); i++) {
                String groupId = KHUtil.GetSFJV(res_ObjectArr.getJSONObject(i), "group");
                String artifactId = KHUtil.GetSFJV(res_ObjectArr.getJSONObject(i), "name");
                String version = KHUtil.GetSFJV(res_ObjectArr.getJSONObject(i), "version");
                // log.debug("获取Maven依赖组件坐标 Loop:" + res_ObjectArr.getJSONObject(i).toString());

                if (artifactIdSimple.equals(artifactId)) {
                    String Maven_Info = "<dependency>\n<groupId>" + groupId + "</groupId>\n<artifactId>" + artifactId + "</artifactId>\n<version>" + versionSimple + "</version>\n</dependency>";
                    log.debug("获取Maven依赖组件坐标 找到目标:" + Maven_Info);

                    jt.update("update ms_it_comment set groupId= ?,artifactId= ?,version= ?,Maven_Info= ? where 开源组件包坐标=?", groupId, artifactId, versionSimple, res_ObjectArr.getJSONObject(i).toString(), jarFile);
                    break;
                }

            }

            Thread.sleep(1000);

        }

    }

    // Fix-获取Maven依赖组件坐标
    public static void MavenFixDataALY() throws Exception {
        String dealSQL = "select distinct 开源组件包坐标 from ms_it_comment where groupId is null";
        List<Map<String, Object>> selectCus = jt.queryForList(dealSQL);
        log.debug("Fix-获取Maven依赖组件坐标 :" + selectCus.size());

        // 获取Maven依赖组件坐标
        for (Map<String, Object> CusMap : selectCus) {
            String jarName = KHUtil.GetMapV(CusMap, "开源组件包坐标");

            // 去掉版本号,获取不必定准确的artifactId,用于关键字搜索
            String artifactIdSimple = jarName.replaceAll("[-|_]\\d.*", "");
            // 获取不必定准确的version,用于搜索结果筛选
            String versionSimple = jarName.substring(artifactIdSimple.length()).replaceFirst("[-|_]", "").replaceAll(".jar", "");

            String get_Json = Util.doHttpGet("https://developer.aliyun.com/artifact/aliyunMaven/searchArtifactByWords?repoId=all&queryTerm=" + KHUtil.encode(artifactIdSimple) + "&_input_charset=utf-8", "UTF-8", 120000, 0);
            log.debug("获取Maven依赖组件坐标 结果：" + get_Json);

            JSONObject res_Json = JSONObject.fromObject(get_Json);

            JSONArray res_ObjectArr = res_Json.getJSONArray("object");
            for (int i = 0; i < res_ObjectArr.size(); i++) {
                String artifactId = KHUtil.GetSFJV(res_ObjectArr.getJSONObject(i), "artifactId");
                String fileName = KHUtil.GetSFJV(res_ObjectArr.getJSONObject(i), "fileName");
                String groupId = KHUtil.GetSFJV(res_ObjectArr.getJSONObject(i), "groupId");
                String version = KHUtil.GetSFJV(res_ObjectArr.getJSONObject(i), "version");
                log.debug("获取Maven依赖组件坐标 Loop:" + res_ObjectArr.getJSONObject(i).toString());

                if (fileName.equals(jarName) || fileName.equals(jarName.replaceAll(".jar", ".pom"))) {
                    String Maven_Info = "<dependency>\n<groupId>" + groupId + "</groupId>\n<artifactId>" + artifactId + "</artifactId>\n<version>" + version + "</version>\n</dependency>";
                    log.debug("获取Maven依赖组件坐标 找到目标:" + Maven_Info);

                    jt.update("update ms_it_comment set groupId= ?,artifactId= ?,version= ?,Maven_Info= ? where 开源组件包坐标=?", groupId, artifactId, version, Maven_Info, jarName);
                    break;
                }

            }

            Thread.sleep(1000);

        }

    }

}